//
//  MainTabbarController.m
//  CROSBY
//
//  Created by Harvey on 2017/10/3.
//  Copyright © 2017年 Haley. All rights reserved.
//

#import "MainTabbarController.h"

// Crosby
#import "CBMusicViewController.h"
#import "CBLightViewController.h"
#import "CBSettingViewController.h"
#import "CBDeviceViewController.h"

// Glowalker
#import "GWMusicViewController.h"
#import "GWLightViewController.h"
#import "GWModeViewController.h"
#import "GWSettingViewController.h"

#import "BaseNavigationController.h"
#import "ProgressHUD.h"

#import <CoreTelephony/CTCallCenter.h>
#import <CoreTelephony/CTCall.h>

@interface MainTabbarController ()

@property (nonatomic,strong) CTCallCenter *callCenter;

@property (nonatomic, assign) BTPeripheralType peripheralType;
@property (nonatomic, assign) BOOL isInitWithType;

@end

@implementation MainTabbarController

- (instancetype)initWithType:(BTPeripheralType)peripheralType
{
    self = [super init];
    if (self) {
        _isInitWithType = YES;
        _peripheralType = peripheralType;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self initTabBar];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setupLanguage) name:kAppLanguageChangedNotification object:nil];
}

- (BOOL)shouldAutorotate
{
    return [self.selectedViewController shouldAutorotate];
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return [self.selectedViewController supportedInterfaceOrientations];
}

- (void)initTabBar
{
    UITabBar *tabbar = [UITabBar appearance];
    tabbar.tintColor = DefaultColor;
    
    CGFloat imageViewH = kIPhoneX ? 83 : 49;
    UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kWidth, imageViewH)];
    bgImageView.image = [UIImage imageNamed:@"tab_bottom_bg"];
    [tabbar insertSubview:bgImageView atIndex:0];
    
    UITabBarItem *item = [UITabBarItem appearance];
    [item setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor lightGrayColor]} forState:UIControlStateNormal];
    [item setTitleTextAttributes:@{NSForegroundColorAttributeName : DefaultColor} forState:UIControlStateSelected];
}

- (void)setupControllers
{
    CBLightViewController *lightVC = [[CBLightViewController alloc] init];
    [self addChildVC:lightVC title:showText(@"灯光") imageName:@"tab_light_unselect" selectedName:@"tab_light_select"];

    CBMusicViewController *musicVC = [[CBMusicViewController alloc] init];
    [self addChildVC:musicVC title:showText(@"音乐") imageName:@"tab_music_unselect" selectedName:@"tab_music_select"];

    CBSettingViewController *settingVC = [[CBSettingViewController alloc] init];
    [self addChildVC:settingVC title:showText(@"设置") imageName:@"tab_setting_unselect" selectedName:@"tab_setting_select"];
    self.selectedIndex = 2;
}

- (void)setupControllersWithType
{
    if (!_isInitWithType) {
        [self setupControllers];
        return;
    }
    if (_peripheralType == BTPeripheralTypeGlowaler) {
        GWMusicViewController *musicVC = [[GWMusicViewController alloc] init];
        [self addChildVC:musicVC title:showText(@"音乐") imageName:@"tab_music_unselect" selectedName:@"tab_music_select"];
        
        GWLightViewController *lightVC = [[GWLightViewController alloc] init];
        [self addChildVC:lightVC title:showText(@"灯光") imageName:@"tab_light_unselect" selectedName:@"tab_light_select"];
        
        GWModeViewController *modeVC = [[GWModeViewController alloc] init];
        [self addChildVC:modeVC title:showText(@"模式") imageName:@"tab_mode_unselect" selectedName:@"tab_mode_select"];
        
        GWSettingViewController *settingVC = [[GWSettingViewController alloc] init];
        [self addChildVC:settingVC title:showText(@"设置") imageName:@"tab_setting_unselect" selectedName:@"tab_setting_select"];
    } else {
        CBLightViewController *lightVC = [[CBLightViewController alloc] init];
        [self addChildVC:lightVC title:showText(@"灯光") imageName:@"tab_light_unselect" selectedName:@"tab_light_select"];
        
        CBMusicViewController *musicVC = [[CBMusicViewController alloc] init];
        [self addChildVC:musicVC title:showText(@"音乐") imageName:@"tab_music_unselect" selectedName:@"tab_music_select"];
        
        CBSettingViewController *settingVC = [[CBSettingViewController alloc] init];
        [self addChildVC:settingVC title:showText(@"设置") imageName:@"tab_setting_unselect" selectedName:@"tab_setting_select"];
    }
}

- (void)addChildVC:(UIViewController *)VC title:(NSString *)title imageName:(NSString *)imageName selectedName:(NSString *)selectedName
{
    VC.tabBarItem.title = title;
    VC.tabBarItem.image = [UIImage imageNamed:imageName];
    VC.tabBarItem.selectedImage = [UIImage imageNamed:selectedName];
    BaseNavigationController *baseNav = [[BaseNavigationController alloc] initWithRootViewController:VC];
    if ([VC isKindOfClass:[CBSettingViewController class]]) {
        CBDeviceViewController *deviceVC = [[CBDeviceViewController alloc] init];
        deviceVC.hidesBottomBarWhenPushed = YES;
        [baseNav pushViewController:deviceVC animated:YES];
    }
    
    [self addChildViewController:baseNav];
}

#pragma mark - notification events
- (void)setupLanguage
{
    NSUInteger count = self.tabBar.items.count;
    GWLog(@"%ld",count);
    
    if (_peripheralType == BTPeripheralTypeGlowaler) {
        NSArray *titles = @[@"音乐",@"灯光",@"模式",@"设置"];
        for (NSUInteger i = 0; i < count; i++) {
            UITabBarItem *item = self.tabBar.items[i];
            NSString *key = titles[i];
            item.title = showText(key);
        }
    } else {
        NSArray *titles = @[@"灯光",@"音乐",@"设置"];
        for (NSUInteger i = 0; i < count; i++) {
            UITabBarItem *item = self.tabBar.items[i];
            NSString *key = titles[i];
            item.title = showText(key);
        }
    }
}

@end
